package com.android.ailaoshan.collection;

import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.ailaoshan.R;

import java.io.File;
import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class SoundAdapter extends RecyclerView.Adapter<SoundAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<CustomFileInfo> myResourceInfo;

    private final String SOUND_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Sound" + File.separator;

    public SoundAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void notifyDataSetChanged(ArrayList<CustomFileInfo> customFileInfo) {
        this.myResourceInfo = customFileInfo;
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return myResourceInfo == null ? 0 : myResourceInfo.size();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.collection_item_sound, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CustomFileInfo customFileInfo = myResourceInfo.get(position);
        if(customFileInfo != null){

            String fileName = customFileInfo.getMoreFileName().get(0);
            String xDescribe = customFileInfo.getDescribe();
            String describe;
            if(xDescribe.length() >= 10){
                describe = xDescribe.substring(0,10) + "...";
            }else{
                describe = xDescribe;
            }


            String filePath = SOUND_PATH+fileName;
            File file = new File(filePath);
            if(file.exists()){
                if( !(customFileInfo.isUpload()) ){
                    holder.content.setText(describe);
                    holder.mainImage.setVisibility(View.VISIBLE);
                    holder.modifyImage.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mainImage;
        TextView content;
        ImageView modifyImage;

        ViewHolder(View itemView) {
            super(itemView);
            mainImage = itemView.findViewById(R.id.collection_item_image);
            content = itemView.findViewById(R.id.collection_item_description);
            modifyImage = itemView.findViewById(R.id.collection_item_modify);
        }
    }

}